/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LAFPanel.java
 *
 * Created on May 8, 2010, 7:35:51 AM
 */
package com.bff.bjj.ui;

import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.utils.Resources;
import com.bff.startup.StartUpProperties;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Bill
 */
public class LAFPanel extends BJJPanel {

    private DefaultComboBoxModel cbLafModel;
    private List<LAFChangeListener> lafListeners;
    private boolean loading;

    /** Creates new form LAFPanel */
    public LAFPanel() {
        lafListeners = new ArrayList<LAFChangeListener>();
        loading = true;
        try {
            cbLafModel = new DefaultComboBoxModel();
            cbLafModel.addElement(StartUpProperties.LAF_SYSTEM);
            for (String s : StartUpProperties.getAvailableLAFs()) {
                cbLafModel.addElement(s);
            }

            if (StartUpProperties.getInstance().getLaf() != null) {
                cbLafModel.setSelectedItem(StartUpProperties.getInstance().getLaf());
            } else {
                cbLafModel.setSelectedItem(StartUpProperties.LAF_DEFAULT);
            }
        } finally {
            loading = false;
        }
        initComponents();
    }

    /**
     * Adds a {@link LAFChangeListener} to this object to receive
     * {@link LAFChangeEvent}s.
     * @param lcl the LAFChangeListener to add
     */
    public synchronized void addLAFChangeListener(LAFChangeListener lcl) {
        lafListeners.add(lcl);
    }

    /**
     * Removes a {@link LAFChangeListener} from this object.
     * @param lcl the LAFChangeListener to remove
     */
    public synchronized void removeLAFChangeListener(LAFChangeListener lcl) {
        lafListeners.remove(lcl);
    }

    protected synchronized void fireLafChangeEvent(String className) {
        if (!loading) {
            LAFChangeEvent fce = new LAFChangeEvent(this, LAFChangeEvent.LAF_CHANGED, className);

            for (LAFChangeListener ll : lafListeners) {
                ll.lafChanged(fce);
            }
        }
    }

    public String getSelectedLAF() {
        return cbLaf.getSelectedItem().toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        cbLaf = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        cbLaf.setModel(cbLafModel);
        cbLaf.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbLafItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 2, 2);
        add(cbLaf, gridBagConstraints);

        jLabel5.setText(getResource(Resources.LABEL_KEY.LAF_TITLE));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        add(jLabel5, gridBagConstraints);

        jLabel6.setFont(jLabel6.getFont().deriveFont(jLabel6.getFont().getSize()+1f));
        jLabel6.setText(getResource(Resources.LABEL_KEY.LAF_DETAILS));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        add(jLabel6, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void cbLafItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbLafItemStateChanged
        StartUpProperties.getInstance().saveSettings( 
                cbLaf.getSelectedItem().toString());
        fireLafChangeEvent(StartUpProperties.getInstance().getLAFClassName());
    }//GEN-LAST:event_cbLafItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbLaf;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables
}
